<template>
  <div>{{messages}}</div>
</template>

<script>
import { dingTalkQrLogin, bindDingTalk } from '../../api/modules/login/login'
import message from '../../utils/message'
import { goLogin } from '../../router'
import { saveLoginInfo } from '../../api/modules/login/saveLoginInfo'

export default {
  name: 'DingTalkAuthentication',
  data () {
    return {
      formValidate: {
        code: '',
        state: ''
      },
      formValidate2: {
        userAccount: '',
        rsaKey: '',
        password: ''
      },
      messages: ''
    }
  },
  created () {
    this.formValidate.code = this.$route.query.code
    this.formValidate.state = this.$route.query.state
    if (this.formValidate.code == null || this.formValidate.state == null) {
      message.message.error('认证参数异常')
      return
    }
    if (this.formValidate.state === 'DingTalkLogion') {
      // 钉钉扫码登录
      dingTalkQrLogin(this.formValidate).then(res => {
        saveLoginInfo(res)
        this.$Spin.show({
          render: (h) => {
            return h('div', [
              h('Icon', {
                class: 'demo-spin-icon-load',
                props: {
                  type: 'ios-loading',
                  size: 18
                }
              }),
              h('div', '正在拼命给您加载中')
            ])
          }
        })
        setTimeout(() => {
          this.$Spin.hide()
        }, 3000)
      }).catch(error => {
        message.message.error((error.message || '系统错误') + ',3秒后将为你跳转到登录页', {
          duration: 3
        }, () => {
          goLogin()
        })
      })
    } else if (this.formValidate.state === 'dingTalk') {
      // 钉钉绑定
      bindDingTalk(this.formValidate).then(res => {
        message.message.success(res.message + '，正在为你返回原页面', {}, () => {
          this.$router.push({ path: '/personSetting' })
        })
      })
    }
  }
}
</script>

<style scoped>
  .demo-spin-icon-load{
    animation: ani-demo-spin 1s linear infinite;
  }
</style>
